package com.hongsen.cn.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.databinding.DataBindingUtil;

import com.hongsen.cn.Utils.LogUtil;
import com.hongsen.cn.Utils.MyTouchListener;
import com.hongsen.cn.Utils.RxBus;
import com.hongsen.cn.Utils.Tools;
import com.hongsen.cn.application.DayAndMonthApplication;
import com.hongsen.cn.base.BaseActivity;
import com.hongsen.cn.bean.RxValue;
import com.hongsen.cn.contract.UnClockView;
import com.hongsen.cn.dayandmonth.R;
import com.hongsen.cn.dayandmonth.databinding.ActivityUnclockBinding;
import com.hongsen.cn.presenter.UnClockPresenter;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

import static com.hongsen.cn.bean.RxBusEventKey.BASE_TITLE_CLICK;
import static com.hongsen.cn.bean.RxBusEventKey.FINISH_UNCLICK_ACTIVITY;

public class UnClockActivity extends BaseActivity<UnClockPresenter> implements UnClockView {

   public ActivityUnclockBinding binding;
    private ArrayList<MyTouchListener> myTouchListeners = new ArrayList<>();
    private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0, btnClear, btnUnlock, btnBack;
    private EditText search_et_input;


    @Override
    protected UnClockPresenter createPresenter() {
        return new UnClockPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(this),R.layout.activity_unclock,null,false);
        binding.setActivity(this);
        baseFl.addView(binding.getRoot());

        RxBus.getDefault()
                .toObservable(RxValue.class)
                .observeOn(AndroidSchedulers.mainThread()).
                subscribe(rxValue -> {
                    if (rxValue.getKey().equals(FINISH_UNCLICK_ACTIVITY)) {
                        LogUtil.e("人脸抓拍登录成功");
                        finish();
                    }
                });

        search_et_input = findViewById(R.id.search_et_input);
        btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "1";
                search_et_input.setText(tmp);
            }
        });
        btn2 = findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "2";
                search_et_input.setText(tmp);
            }
        });
        btn3 = findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "3";
                search_et_input.setText(tmp);
            }
        });
        btn4 = findViewById(R.id.btn4);
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "4";
                search_et_input.setText(tmp);
            }
        });
        btn5 = findViewById(R.id.btn5);
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "5";
                search_et_input.setText(tmp);
            }
        });
        btn6 = findViewById(R.id.btn6);
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "6";
                search_et_input.setText(tmp);
            }
        });
        btn7 = findViewById(R.id.btn7);
        btn7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "7";
                search_et_input.setText(tmp);
            }
        });
        btn8 = findViewById(R.id.btn8);
        btn8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "8";
                search_et_input.setText(tmp);
            }
        });
        btn9 = findViewById(R.id.btn9);
        btn9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "9";
                search_et_input.setText(tmp);
            }
        });
        btn0 = findViewById(R.id.btn0);
        btn0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String tmp = search_et_input.getText().toString().trim();
                tmp = tmp + "0";
                search_et_input.setText(tmp);
            }
        });

        btnClear = findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search_et_input.setText("");
            }
        });
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btnUnlock = findViewById(R.id.btnUnlock);
        btnUnlock.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtils.equals(search_et_input.getText().toString(), "888888")) {
                    DayAndMonthApplication.getInstance().setLockFlag(false);
                    DayAndMonthApplication.getInstance().setNoOperationTimes(0);
                    finish();
                } else {
                    Tools.dispToast("密码不正确,请确认");
                }
            }
        });

    }
}
